package com.pd.business.impl;

import com.pd.business.UserService;
import com.pd.dao.mapper.UserDao;
import com.pd.dao.model.User;
import com.pd.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by dong peng
 * DATE : 2017/2/26
 * TIME : 21:30.
 */

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    /**
     * 查询用户数据
     *
     * @return
     */
    public List<User> query(){
        List<User> users = userDao.selectUsers();
        return users;
    }

    /**
     * 增加用户数据
     * @param user
     * @return
     */
    public Integer add(User user){
        if(StringUtils.isEmpty(user.getAccount())) {
            throw new BusinessException("账号必须填写");
        }

        if(StringUtils.isEmpty(user.getPasswd())) {
            throw new BusinessException("密码必须填写");
        }

        User existUser =userDao.selectUserByAccount(user);
        if(existUser != null) {
            throw  new BusinessException("该用户已经注册");
        }
        int result = userDao.insertUser(user);
        return result;
    }

    /**
     * 修改用户数据
     *
     * @param user
     * @return
     */
    public Integer modify(User user){
        int result = userDao.updateUser(user);
        return result;
    }

    /**
     * 删除用户数据
     *
     * @param id
     * @return
     */
    public Integer delete(Integer id){
        int result = userDao.deleteUser(id);
        return result;
    }
}
